package com.hunter.sign.server.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.hunter.common.base.BaseController;
import com.hunter.common.util.PropertiesFileUtil;
import com.hunter.sign.common.constant.SignResult;
import com.hunter.sign.common.constant.SignResultConstant;
import com.hunter.sign.common.util.ParamProcess;
import com.hunter.sign.common.util.biometrics.BmpConver;
import com.hunter.sign.common.util.biometrics.CFingerUtils;
import com.hunter.sign.dao.model.SignWitness;
import com.hunter.sign.dao.model.SignWitnessExample;
import com.hunter.sign.rpc.api.SignWitnessService;
import com.hunter.sign.server.vo.request.WitnessRegisterReq;


/**
 * 见证员
 * 
 * @author hunter
 * 
 */
@Controller
@RequestMapping(value = "/witness")
public class WitnessController extends BaseController {

	private Logger logger = LoggerFactory.getLogger(WitnessController.class);

	@Autowired
	private SignWitnessService signWitnessService;

	/**
	 * 注册
	 * 
	 * @return
	 */
	@RequestMapping(value = "/register", method = RequestMethod.POST)
	public void register(HttpServletRequest request, HttpServletResponse response, WitnessRegisterReq witnessRegisterReq) {
		logger.info("见证员注册，req："+witnessRegisterReq);
		// 参数校验失败
		if (!ParamProcess.paramValid(witnessRegisterReq, WitnessRegisterReq.class)) {
			outHttpJson(response, new SignResult(SignResultConstant.FAILED, null));
			return;
		}
		String dsetImg = witnessRegisterReq.getFingerprintImg().replace(".bmp", "")+"conver.bmp";
	    BmpConver.conver(witnessRegisterReq.getFingerprintImg(), dsetImg);
		//指纹识别
		if(CFingerUtils.match(dsetImg, dsetImg)<Double.valueOf(PropertiesFileUtil.getInstance("config").get("finger.match"))){
			outHttpJson(response, new SignResult(SignResultConstant.UNRECOGNIZED_WITNESS_FINGER, null));
			return;
		}
		logger.info("见证员注册，参数校验成功");
		//通过证件号检索用户
		SignWitnessExample example = new SignWitnessExample();
		example.createCriteria()
        	.andIdTypeEqualTo(witnessRegisterReq.getIdType())
        	.andIdCardEqualTo(witnessRegisterReq.getIdCard());
		SignWitness signWitness = signWitnessService.selectFirstByExample(example);
		//用户已注册
		if(signWitness != null){
			outHttpJson(response, new SignResult(SignResultConstant.ALREGISTER_USER, null));
			return;
		}
		logger.info("见证员注册，开始保存图片");
		//注册用户
		signWitness = new SignWitness();
		signWitness.setRealname(witnessRegisterReq.getRealname());
		signWitness.setIdType(witnessRegisterReq.getIdType());
		signWitness.setIdCard(witnessRegisterReq.getIdCard());
		signWitness.setIdFront(witnessRegisterReq.getIdFront());
		signWitness.setIdBack(witnessRegisterReq.getIdBack());
		signWitness.setVideoInfo(witnessRegisterReq.getVideoInfo());
		signWitness.setFingerprintImg(dsetImg);
		signWitness.setFingerprintVerify(witnessRegisterReq.getFingerprintVerify());
		signWitness.setCreateTime(System.currentTimeMillis());
		signWitnessService.insertSelective(signWitness);
		logger.info("见证员注册成功");
		outHttpJson(response, new SignResult(SignResultConstant.SUCCESS, null));
	}
	
}
